<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_alumni extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'a.kd_prodi';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		$kd_fakultas = isset($_POST['kd_fakultas']) ? strval($_POST['kd_fakultas']) : '';
		$kd_prodi = isset($_POST['kd_prodi']) ? strval($_POST['kd_prodi']) : '';
		$tahun = isset($_POST['tahun']) ? strval($_POST['tahun']) : '';
		$nim = isset($_POST['nim']) ? strval($_POST['nim']) : '';
		$nama = isset($_POST['nama']) ? strval($_POST['nama']) : '';
		$kd_kabup = isset($_POST['kd_kabup']) ? strval($_POST['kd_kabup']) : '';
		$kd_dosen = isset($_POST['kd_dosen']) ? strval($_POST['kd_dosen']) : '';
		
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			//$this->db->select('a.kd_mahasiswa, a.thn_angkatan, a.nim, a.nama, a.alamat, a.no_telp, b.nama_prodi, c.nama_dosen, a.nama_ortu, a.alamat_ortu, a.pekerjaan, a.telp_ortu, a.status');
			$this->db->select('a.kd_mahasiswa, a.thn_angkatan, a.nim, a.nama, a.alamat, a.no_telp, a.nama_ortu, a.alamat_ortu, a.pekerjaan, a.telp_ortu, a.status, a.status_kerja,a.instansi,a.tahun_lulus');
			$this->db->from('tbl_mahasiswa a');
			$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
			$this->db->join('ref_dosen c', 'c.kd_dosen = a.kd_dosen','left');
			
			#Kimochi - otoritas
				#--Faklutas
			if($this->session->userdata('kd_fakultas')!=''){
				$this->db->where('b.kd_fakultas', $this->session->userdata('kd_fakultas'));
			}else{
				if($kd_fakultas != '')
					$this->db->where('b.kd_fakultas', $kd_fakultas);
			}
				#--Prodi
			if($this->session->userdata('kd_prodi')!=''){
				$this->db->where('a.kd_prodi', $this->session->userdata('kd_prodi'));
			}else{
				if($kd_prodi != '')
					$this->db->where('a.kd_prodi', $kd_prodi);
			}
			#End Kimochi - otoritas
			
			// if($kd_fakultas != ''){
				// $this->db->where('b.kd_fakultas', $kd_fakultas);
			// }
			// if($kd_prodi != ''){
				// $this->db->where('a.kd_prodi', $kd_prodi);
			 //}
			 if($tahun != ''){
				 $this->db->where('a.thn_angkatan', $tahun);
			 }
			 //if($tahun != ''){
				// $this->db->where('a.kd_dosen', $kd_dosen);
			//}
			
				#-- Kabupaten
			if($kd_kabup != ''){
				$this->db->where('a.kota', $kd_kabup);
			}
			
				#-- dosen
			if($kd_dosen != ''){
				$this->db->where('a.kd_dosen', $kd_dosen);
			}
			
			if($nim != ''){
				$this->db->where('a.nim', $nim);
			}
			
			if($nama != ''){
				$this->db->like('nama', $nama, 'both');
			}
			$this->db->where('a.status','Lulus');
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function getdataedit($kode){
		$this->db->flush_cache();
		$this->db->select('a.kd_mahasiswa, a.thn_angkatan, a.nim, a.nama, a.alamat, a.no_telp, a.nama_ortu, a.alamat_ortu, a.pekerjaan, a.telp_ortu, a.status, a.kd_prodi,a.status_kerja,a.instansi');
		$this->db->select(' b.nama_prodi, d.kd_dosen, d.nama_dosen');
		$this->db->select(' c.kd_fakultas, c.nama_fakultas');
		$this->db->from('tbl_mahasiswa a');
		$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
		$this->db->join('ref_fakultas c', 'c.kd_fakultas = b.kd_fakultas','left');
		$this->db->join('ref_dosen d', 'd.kd_dosen = a.kd_dosen','left');
		
		$this->db->where('a.kd_mahasiswa', $kode);
		return $this->db->get();
	}
	
	function getdataByNIM($nim){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('tbl_mahasiswa a');
		$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
		$this->db->join('ref_fakultas c', 'c.kd_fakultas = b.kd_fakultas');
		$this->db->where('a.nim', $nim);
		
		return $this->db->get();
	}
	
	function togrid($data, $count=0){
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
			$response->total = $count;
		}else{
		$response->rows = array();
		}
		return json_encode($response);
	}
	
	function getDataCount($status=''){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('tbl_mahasiswa');
		
		if($status=='ma'){
			$this->db->where('status', 'Mahasiswa Aktif');
		}
		return $this->db->get()->num_rows();
	}
	
	function getDataChart($data){
		$i=0;
		foreach($data as $d){
			$this->db->flush_cache();
			$this->db->select('*');
			$this->db->from('tbl_mahasiswa');
			$this->db->where('thn_angkatan', $d);
			
			$tmp[$i] = $this->db->get()->num_rows();
			$i++;
		}
		return $tmp;
	}
	
	
	function isExistKode($kode){
		if ($kode!=null)
			$this->db->where('kd_mahasiswa',$kode);
		
		$this->db->select('*');
		$this->db->from('tbl_mahasiswa');
		$query = $this->db->get();
		
		$rs = $query->num_rows() ;		
		$query->free_result();
		
		return ($rs>0);
	}
	function InsertOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('nim', $data['nim']);
		$this->db->set('status', $data['status']);
		$this->db->set('status_kerja', $data['status_kerja']);
		$this->db->set('instansi', $data['instansi']);
		$this->db->set('tahun_lulus', $data['tahun_lulus']);
		$result = $this->db->insert('tbl_mahasiswa');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function UpdateOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('nim', $data['nim']);
		$this->db->set('status', $data['status']);
		$this->db->set('status_kerja', $data['status_kerja']);
		$this->db->set('instansi', $data['instansi']);
		$this->db->set('tahun_lulus', $data['tahun_lulus']);
		$this->db->where('kd_mahasiswa', $data['kd_mahasiswa']);
		$result = $this->db->update('tbl_mahasiswa');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function DeleteOnDb($kode){
		$this->db->flush_cache();
		$this->db->where('kd_mahasiswa', $kode);
		$result = $this->db->delete('tbl_mahasiswa'); 
		
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	/*function get_pdf(){
		# get data
		$this->db->flush_cache();		
		$this->db->select('a.kd_mahasiswa, a.thn_angkatan, a.nim, a.nama, a.alamat, a.no_telp, b.nama_prodi, c.nama_dosen, a.nama_ortu, a.alamat_ortu, a.pekerjaan, a.telp_ortu, a.status');
		$this->db->from('tbl_mahasiswa a');
		$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
		$this->db->join('ref_dosen c', 'c.kd_dosen = a.kd_dosen','left');

		// proses
		$result = $this->db->get();
		
		# init column
		$pdfdata['column'] = array();
		array_push($pdfdata['column'], array('caption'=>'No.', 'width'=>10, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'NIM', 'width'=>20, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Nama Mahasiswa', 'width'=>35, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Alamat', 'width'=>35, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Telepon', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Prodi', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Dosen Wali', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Nama Orang Tua', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Pekerjaan', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Status', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		
		# init record
		$i=1;
		$pdfdata['row'] = array();
		foreach($result->result() as $row){
			array_push($pdfdata['row'], array(
				$i,
				$row->nim,
				$row->nama,
				$row->alamat,
				$row->no_telp,
				$row->nama_prodi,
				$row->nama_dosen,
				$row->nama_ortu,
				$row->pekerjaan,
				$row->status
			));
			$i++;
		}
		
		return $pdfdata;
		
	}*/
	
	//PDF
	function get_pdf($fil){
		
		# get data
		$this->db->flush_cache();
		$this->db->start_cache();
		//$this->db->select('a.kd_mahasiswa, a.thn_angkatan, a.nim, a.nama, a.alamat, a.no_telp, b.nama_prodi, d.kd_dosen, d.nama_dosen, a.nama_ortu, a.alamat_ortu, a.pekerjaan, a.telp_ortu, a.status, a.kd_prodi,
		$this->db->select('a.kd_mahasiswa, a.thn_angkatan, a.nim, a.nama, a.alamat, a.no_telp, a.nama_ortu, a.alamat_ortu, a.pekerjaan, a.telp_ortu, a.status, a.status_kerja,a.instansi,a.tahun_lulus');
		$this->db->select(' b.nama_prodi');
		$this->db->select(' c.kd_fakultas, c.nama_fakultas');
		$this->db->from('tbl_mahasiswa a');
		$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
		$this->db->join('ref_fakultas c', 'c.kd_fakultas = b.kd_fakultas','left');
		$this->db->join('ref_dosen d', 'd.kd_dosen = a.kd_dosen','left');
					
			$this->db->where('a.kd_prodi', $fil['kd_prodi']);
			$this->db->where('a.thn_angkatan', $fil['thn_angkatan']);
			$this->db->where('a.status', 'Lulus');
			
			//$this->db->where('a.status', 'A');

		// proses
		$result = $this->db->get();
		
		if ($result->num_rows() > 0) {
            foreach ($result->result() as $data) {
                $data_pdf[] = $data;
            }
            return $data_pdf;           
        }
	}
	
}

?>